package com.hsc.educenter.controller;


import com.hsc.commonutils.JwtUtils;
import com.hsc.commonutils.R;
import com.hsc.commonutils.ordervo.UcenterMemberOrder;
import com.hsc.educenter.entity.UcenterMember;
import com.hsc.educenter.entity.vo.RegisterVo;
import com.hsc.educenter.entity.vo.UcenterMemberVo;
import com.hsc.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-10-17
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin //gateway解决了可以注掉
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录方法
     * TODO @requestBody得配合postmappering才能够使用
     * @param member 封装的手机号和密码
     * @return
     */
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        //调用service方法实现登录
        //返回token值
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    /**
     * 注册方法
     * @param registerVo 注册封装信息
     * @return
     */
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据token获取用户信息
     * @param request request,前端会把token存到request中
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        //可以优化
        /*UcenterMember member1 = new UcenterMember();
        member1.setAvatar(member.getAvatar());
        member1.setNickname(member.getNickname());
        member1.setMobile(member.getMobile());*/
        return R.ok().data("userInfo",member);
    }

    /**
     * 根据用户id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("getUserById/{Id}")
    public R getUserById(@PathVariable String id){
        UcenterMember user = memberService.getById(id);
        return R.ok().data("userInfo",user);
    }

    /**
     * 根据用户id获取用户信息，生产者
     * @param id
     * @return
     */
    @PostMapping("getInfoUc/{id}")
    public UcenterMemberVo getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMemberVo memeber = new UcenterMemberVo();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }

    /**
     * 根据用户id获取用户信息
     * @param id 用户id
     * @return
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        //对象转化
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    /**
     * 查询某一天注册人数 生产者
     * @param day 日期
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }


}

